﻿Imports System.Configuration

Public Class Config

    Private Sub ButtonOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOK.Click
        Dim connectionstring As String
        Dim config As Configuration
        Dim NrRighe As Integer
        Dim Colonna As Integer
        Dim NomeColonna As String
        Dim Visibile As Boolean

        connectionstring = "Data Source=" & Server.Text & ";Initial Catalog=" & Database.Text & ";Integrated Security=True;User ID=" & Utente.Text & ";Password=" & Password.Text
        'connectionstring = "Data Source=" & Server.Text & ";Initial Catalog=" & Database.Text & _
        '    ";Persist Security Info=True;User ID=" & Utente.Text & ";Password=" & Password.Text

        config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        config.ConnectionStrings.ConnectionStrings("FormFlussi.My.MySettings.CM_DBConnectionString").ConnectionString = connectionstring

        config.Save(ConfigurationSaveMode.Modified, True)

        ConfigurationManager.RefreshSection("connectionStrings")

        My.Settings.FreqAggiornamento = Convert.ToDecimal(FreqAgg.Text)

        If Convert.ToInt32(NumCarrInizio.Text) >= Convert.ToInt32(NumCarrFine.Text) Then
            MsgBox("Il numero iniziale di carrello deve essere minore del numero finale")
            NumCarrInizio.Focus()
            Exit Sub
        End If

        My.Settings.NumeroCarrelloInizio = Convert.ToInt32(NumCarrInizio.Text)
        My.Settings.NumeroCarrelloFine = Convert.ToInt32(NumCarrFine.Text)
        My.Settings.Debug = Convert.ToBoolean(ModDebug.Text)

        My.Settings.Save()

        NrRighe = DataGridColonne.Rows.Count


        For x As Integer = 0 To NrRighe - 1
            Colonna = DataGridColonne.Rows(x).Cells(0).Value
            NomeColonna = DataGridColonne.Rows(x).Cells(1).Value
            Visibile = DataGridColonne.Rows(x).Cells(2).Value

            If NomeColonna <> "" Then


                Principale.GridEX1.RootTable.Columns(Colonna).Visible = Visibile
            End If
        Next

        Principale.GridEX1.SaveSettings = True

        If (Principale.AbleRrestart = True) Then
            Principale.Restart = True
        End If

        Me.Close()

    End Sub

    Private Sub ButtonAnnulla_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAnnulla.Click
        Dim risultato As Integer = MsgBox("Vuoi uscire senza salvare?", vbYesNo)

        If risultato = 6 Then
            Principale.Show()
            Me.Close()

        End If
    End Sub

    Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim connectionstring As String = System.Configuration.ConfigurationManager.ConnectionStrings("FormFlussi.My.MySettings.CM_DBConnectionString").ConnectionString
        Dim split As String() = connectionstring.Split(New [Char]() {";"})


        For Each s As String In split
            If s.StartsWith("Data Source") = True Then
                Server.Text = s.Substring(12, s.Length - 12)
            End If
            If s.StartsWith("Initial Catalog") = True Then
                Database.Text = s.Substring(16, s.Length - 16)
            End If
            If s.StartsWith("User ID") = True Then
                Utente.Text = s.Substring(8, s.Length - 8)
            End If
            If s.StartsWith("Password") = True Then
                Password.Text = s.Substring(9, s.Length - 9)

            End If
        Next s

        FreqAgg.Text = My.Settings.FreqAggiornamento
        NumCarrInizio.Text = My.Settings.NumeroCarrelloInizio
        NumCarrFine.Text = My.Settings.NumeroCarrelloFine
        ModDebug.Text = My.Settings.Debug


        LeggiCampiGrid()



    End Sub

    Private Sub LeggiCampiGrid()


        Dim NumColonne As Integer


        Setup.Tables("Colonne").Rows.Clear()


        NumColonne = Principale.GridEX1.RootTable.Columns.Count


        Try

            For x As Integer = 0 To NumColonne - 1

                If Principale.GridEX1.RootTable.Columns(x).Tag = True Then
                    Dim Riga() As String = {Convert.ToString(x), Principale.GridEX1.RootTable.Columns(x).Caption, Convert.ToString(Principale.GridEX1.RootTable.Columns(x).Visible)}

                    Setup.Tables("Colonne").Rows.Add(Riga)

                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message)


        End Try

        DataGridColonne.Columns(0).Visible = False

    End Sub

    Private Sub NumCarrInizio_Leave(sender As System.Object, e As System.EventArgs) Handles NumCarrInizio.Leave
        Try
            Dim NumCarrelloInizio = Convert.ToInt32(NumCarrInizio.Text)
            Dim NumCarrelloFine = Convert.ToInt32(NumCarrFine.Text)

            If NumCarrelloInizio >= NumCarrelloFine Then
                MsgBox("Il numero iniziale di carrello deve essere minore o uguale al numero finale")
                NumCarrInizio.Focus()

            End If
        Catch ex As Exception
            MsgBox("Inserire un numero!")
            NumCarrInizio.Focus()

        End Try



        Exit Sub
    End Sub

    Private Sub NumCarrFine_Leave(sender As System.Object, e As System.EventArgs) Handles NumCarrFine.Leave
        Try
            Dim NumCarrelloInizio = Convert.ToInt32(NumCarrInizio.Text)
            Dim NumCarrelloFine = Convert.ToInt32(NumCarrFine.Text)

            If NumCarrelloInizio >= NumCarrelloFine Then
                MsgBox("Il numero iniziale di carrello deve essere minore o uguale al numero finale")
                NumCarrFine.Focus()

            End If
        Catch ex As Exception
            MsgBox("Inserire un numero!")
            NumCarrFine.Focus()

        End Try



        Exit Sub
    End Sub


    Private Sub Annulla_Btn_Click(sender As System.Object, e As System.EventArgs) Handles Annulla_Btn.Click
        Dim risultato As Integer = MsgBox("Vuoi chiudere il programma?", vbYesNo)

        If risultato = 6 Then
            Application.Exit()
        End If
    End Sub
End Class